/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package operators;

import genotypes.Angles;
import genotypes.AnglesPopulation;
import java.util.Arrays;
import java.util.Random;

/**
 *
 * @author rusak
 */
public class TwiceMoreOrLessMutation implements MutationOperator {
    Random r = new Random();
    private double mutationChance;

    public TwiceMoreOrLessMutation(double mutationChance) {
        this.mutationChance = mutationChance;
    }
    
    
    @Override
    public AnglesPopulation mutate(AnglesPopulation population, double[][] limitations) {
                  int size = population.getSize();
        int indSize = population.getAnglesAt(0).getAngles().length;
        int where;
        Angles newAngles[] = new Angles[size];
        double angles[];

        for (int i = 0; i < size; i++) {


            angles = Arrays.copyOf(population.getAnglesAt(i).getAngles(), indSize);
            
            if (r.nextDouble() < mutationChance) {
                where = r.nextInt(indSize);
                if(r.nextDouble() < 0.5) {
                    angles[where] /= 2;
                }
                else {
                    angles[where] *= 2;
                }
                angles[where] %= 2*Math.PI;
                if (angles[where] < limitations[where][0]) {
                    angles[where] = limitations[where][0];
                } else if (angles[where] > limitations[where][1]) {
                    angles[where] = limitations[where][1];
                }
            }
            newAngles[i] = new Angles(angles);
        }
        return new AnglesPopulation(newAngles);            
    }
    
    
}
